package main

import (
	"hoock/helper"
	"log"
	"os/exec"

	"github.com/gin-gonic/gin"
)

func main() {

	//app 加载配置
	aconfigs := helper.GetAppConfig("app.json")

	//定时加载 配置文件
	// go func(a *helper.App) {
	// 	for {
	// 		a = helper.GetAppConfig("app.json")
	// 		log.Println("the app config is :")
	// 	}
	// }(aconfigs)

	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		var req helper.Request

		//加载git hoock 命令
		aconfigs = helper.GetAppConfig("app.json")
		//err := c.Bind(&req)
		err := c.ShouldBind(&req)
		if err != nil {
			log.Println("misssing the param falg ", err)
		} else {
			if req.Flag != "" {
				//找到相应的命令
				cmd, ok := aconfigs.Cmds[req.Flag]
				if ok {
					log.Println("this is the command :", cmd.Cmd)
					//执行相应的命令
					hand := exec.Command("/bin/bash", "-c", "'" + cmd.Cmd + "'")
                    bytes,err := hand.Output()
                    if err != nil {
                     log.Println("the out put err is :" ,err)
                    }
                    resp := string(bytes)
                    log.Println("the respone is :" ,resp)    
                    //log.Println("the exec result is :",re)
				}
			}
		}
		log.Println("the request  is :", req)
		c.JSON(200, gin.H{
			"message": aconfigs,
		})
	})
	r.Run(aconfigs.Port) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
